package com.zhongyewx.teachercert.framework.arouter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.annotation.Interceptor;
import com.alibaba.android.arouter.facade.callback.InterceptorCallback;
import com.alibaba.android.arouter.facade.template.IInterceptor;
import com.alibaba.android.arouter.launcher.ARouter;
import com.zhongyewx.teachercert.api.ZYARouterApi;
import com.zhongyewx.teachercert.framework.exception.UnLoginException;
import com.zhongyewx.teachercert.utils.ZYSharedPreferencesUtils;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;


/**
 * 登录拦截器
 */
@Interceptor(priority = 1)
public class LoginInterceptor implements IInterceptor {
    @SuppressLint("CheckResult")
    @Override
    public void process(final Postcard postcard, InterceptorCallback callback) {
        if (postcard.getExtra() != ZYARouterApi.AROUTER_NEED_LOGIN || ZYSharedPreferencesUtils.getBooleanValue("isLogin")) {
            callback.onContinue(postcard);
//            Observable.just(0).observeOn(AndroidSchedulers.mainThread()).subscribe(new io.reactivex.functions.Consumer<Integer>() {
//                @Override
//                public void accept(Integer integer) throws Exception {
//                    ARouter.getInstance()
//                            .build(postcard.getPath())
//                            //拦截跳转的时候，后续登录成功后，继续跳转，需要这个url
//                            .greenChannel()
////                            .withFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
//                            .navigation();
//                }
//            });
        } else {
            callback.onInterrupt(new UnLoginException("用户未登录"));
//            Observable.just(0).observeOn(AndroidSchedulers.mainThread()).subscribe(new io.reactivex.functions.Consumer<Integer>() {
//                @Override
//                public void accept(Integer integer) throws Exception {
//                    ARouter.getInstance()
//                            .build(ZYARouterApi.AROUTER_LOGIN_ACTY)
//                            //拦截跳转的时候，后续登录成功后，继续跳转，需要这个url
//                            .greenChannel()
////                            .withFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
//                            .navigation();
//                }
//            });
        }
    }

    @Override
    public void init(Context context) {

    }
}
